/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2021，所有权利保留。
 * <p>
 * 项目名：	design-pattern 文件名：	PrototypeWukong 模块说明： 修改历史： 2021/7/22 0022 - lizhixing - 创建。
 */
package com.lovely602.design.creation.prototype.demo;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author lizhixing
 */
public class PrototypeWuKong {

  public static void main(String[] args) {
    JFrame jf = new JFrame("原型模式测试");
    jf.setLayout(new GridLayout(1, 2));
    Container contentPane = jf.getContentPane();
    WuKong obj1 = new WuKong();
    contentPane.add(obj1);
    WuKong obj2 = (WuKong) obj1.clone();
    contentPane.add(obj2);
    jf.pack();
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

class WuKong extends JPanel implements Cloneable {

  private static final long serialVersionUID = -9072551271851795923L;

  public WuKong() {
    try {
      InputStream is = ClassLoader.getSystemResourceAsStream("image/wukong.jpg");
      Image image = ImageIO.read(is);
      JLabel jLabel = new JLabel(new ImageIcon(image));
      this.add(jLabel);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override
  protected Object clone() {
    WuKong w = null;
    try {
      w = (WuKong) super.clone();
    } catch (CloneNotSupportedException e) {
      System.out.println("拷贝悟空失败");
    }
    return w;
  }
}
